﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmDetails : Form
    {
        public FrmDetails()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 影片
        /// </summary>
        public Movie movie = new Movie();

        private void FrmDetails_Load(object sender, EventArgs e)
        {
            InitialData();
            BindDgvAll();
        }

        #region 初始化控件信息
        /// <summary>
        /// 初始化控件信息
        /// </summary>
        private void InitialData()
        {
            pictureBox1.Image = Image.FromFile(movie.MoviePath);
            pictureBox2.Image = Image.FromFile(movie.VideoPath);
            lblName.Text = movie.MovieName;
            lblEngName.Text = movie.EnglishName;
            lblShichang.Text = movie.MovieDuration;
            lblType.Text = movie.MovieType;
            lblDate.Text = movie.BeOnDate.ToShortDateString();
            lblAbout.Text = movie.MovieAbout;

            dateTimePicker1.MinDate = DateTime.Now;

            label13.Parent = pictureBox8;
            DataGridViewButtonColumn button = new DataGridViewButtonColumn();
            button.HeaderText = "选座购票";
            button.Text = "点击购买";
            button.UseColumnTextForButtonValue = true;
            this.dataGridView1.Columns.AddRange(button);
        }
        #endregion

        #region 绑定数据
        List<Hall> listHall = new List<Hall>();
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            listHall.Clear();
            label2.Visible = false;
            DateTime time = dateTimePicker1.Value;
            foreach (Hall item in movie.HallInfo)
            {
                if (item.ShowData.ToShortDateString() == time.ToShortDateString())
                {
                    listHall.Add(item);
                }
            }
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = new BindingList<Hall>(listHall);
            if (dataGridView1.RowCount < 1)
                label2.Visible = true;
         
        }
        #endregion

        //更改时间发生事件
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            BindData();
        }

        #region 收藏 评分
        //想看
        private void button6_Click(object sender, EventArgs e)
        {
            if (RoleManage.user == null)
            {
                MessageBox.Show("尚未登录！","提示：");
                return;
            }
            foreach (Movie item in RoleManage.user.WantMovie)
            {
                if (item.MovieName == movie.MovieName)
                {
                    MessageBox.Show("该影片已收藏！","提示：");
                    return;
                }
            }
            RoleManage.user.WantMovie.Add(this.movie);
            MessageBox.Show("收藏成功！", "提示：");
        }
        #endregion

        //评分
        private void button1_Click(object sender, EventArgs e)
        {
            FrmGrade grade = new FrmGrade();
            grade.movie = this.movie;
            grade.ShowDialog();
        }
        //退出
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 单击DGV事件
        //单击dgv时发生
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                //创建dgv控件列
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    if (RoleManage.user == null&&RoleManage.admin == null)
                    {
                        MessageBox.Show("请先登录！","提示：");
                        return;
                    }
                    string name = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                    DateTime date = dateTimePicker1.Value.Date;
                    string time = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                    foreach (Hall item in movie.HallInfo)
                    {
                        
                        //判断影厅 放映日期 和 时间
                        if (item.H_Name == name && item.ShowTime == time)
                        {
                            FrmBuy buy = new FrmBuy();
                            buy.hall = item;
                            buy.movie = this.movie;
                            buy.Show();
                            break;
                        }
                    }
                   
                }
            }
        }
        #endregion

        /// <summary>
        /// 绑定所有数据
        /// </summary>
        private void BindDgvAll()
        {
            label2.Visible = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = new BindingList<Hall>(movie.HallInfo);
            if (dataGridView1.RowCount < 1)
                label2.Visible = true;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            BindDgvAll();
        }
    }
}
